package com.ac.scum.log.web.model.pack.controller;

import com.ac.common.core.exception.BaseException;
import com.ac.common.core.result.Result;
import com.ac.scum.log.bean.Menu;
import com.ac.scum.log.bean.Package;
import com.ac.scum.log.web.interceptor.anno.PermissionCheck;
import com.ac.scum.log.web.model.menu.service.MenuService;
import com.ac.scum.log.web.model.pack.service.PackageService;
import com.ac.scum.log.web.model.wechat.pay.service.PayPackService;
import com.ac.scum.log.web.model.wechat.pay.service.PayService;
import com.ac.scum.pay.PayPack;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 套餐
 */
@RestController
@RequestMapping("package")
@AllArgsConstructor
public class PackageController {

    private final PayService payService;
    private final MenuService menuService;
    private final PayPackService payPackService;
    private final PackageService packageService;
    private final StringRedisTemplate stringRedisTemplate;

    @GetMapping("page/{current}/{size}")
    @PermissionCheck(permission = "package")
    public Result<Page<Package>> getPackageListPage(@PathVariable("current") Integer current, @PathVariable("size") Integer size) {
        return Result.ok(packageService.page(new Page<>(current, size)));
    }

    @GetMapping("payList")
    public Result<List<PayPack>> getPayList() throws BaseException {
        return Result.ok(payPackService.selectList());
    }

    @PostMapping("pay/{packagePayId}")
    public Result<Object> pay(@PathVariable Long packagePayId, @RequestParam Long serverId) throws BaseException {
        PayPack payPack = payPackService.getById(packagePayId);
        return Result.ok(payService.codePay(payPack, serverId));
    }

    @GetMapping("findPayState")
    public Result<String> findPayState(@RequestParam String key) {
        return Result.ok(stringRedisTemplate.opsForValue().get(key));
    }

    @PutMapping
    @PermissionCheck(permission = "package")
    public Result<Package> modify(@RequestBody Package pack) {
        packageService.updateById(pack);
        return Result.ok(pack);
    }

    @PostMapping
    @PermissionCheck(permission = "package")
    public Result<Package> insert(@RequestBody Package pack) {
        packageService.save(pack);
        return Result.ok(pack);
    }

    @DeleteMapping("{id}")
    @PermissionCheck(permission = "package")
    public Result<Boolean> delete(@PathVariable Long id) {
        return Result.ok(packageService.removeById(id));
    }

    @GetMapping("menu/{packageId}")
    @PermissionCheck(permission = "package")
    public Result<List<Menu>> menuPage(@PathVariable("packageId") Long packageId) {
        return Result.ok(menuService.list(Wrappers.lambdaQuery(Menu.class).inSql(Menu::getId, "select menu_id from package_menu where package_id = " + packageId)));
    }

    @PutMapping("joinMenu")
    @PermissionCheck(permission = "package")
    public Result<Boolean> joinMenu(@RequestParam("packageId") Long packageId, @RequestParam("menuId") Long menuId) {
        return Result.ok(packageService.joinMenu(packageId, menuId));
    }

    @PutMapping("quitMenu")
    @PermissionCheck(permission = "package")
    public Result<Boolean> quitMenu(@RequestParam("packageId") Long packageId, @RequestParam("menuId") Long menuId) {
        return Result.ok(packageService.quitMenu(packageId, menuId));
    }
}
